#include "SStructTypeDescriptor.h"
                       
void SStructTypeDescriptor::AddMember(string name, string description, SType type)
{
	if ( IsDefined(name) )
	{
		throw SArgumentException("There is an entry with the same key.");
	}

	members->push_back( SStructMember::Create(name, description, type) );
}

void SStructTypeDescriptor::AddMember(string name, string description, TypeDescriptorSp typeDescriptor)
{
	if ( IsDefined(name) )
	{
		throw SArgumentException("There is an entry with the same key.");
	}
	if ( !typeDescriptor->IsValid() )
	{
		throw SArgumentException("The type descriptor is invalid.");
	}

	members->push_back( SStructMember::Create(name, description, typeDescriptor) );
}


SStructMemberSp SStructTypeDescriptor::GetMember(int index)
{
	if ( index < 0 || index >= (int) members->size() )
	{
		throw SArgumentException("There is not an entry at this position.");
	}

	return members->at(index);
}

SStructMemberSp SStructTypeDescriptor::GetMember(string name)
{
	BOOST_FOREACH(SStructMemberSp entry, *members)
	{
		 if ( entry ) 
		 {
			 if ( entry->GetName() == name )
			 {
				 return entry;
			 }
		 }
	}

	throw SArgumentException("There is not a member with the name '" + name + "'");
}

bool SStructTypeDescriptor::IsDefined(string name)
{
	BOOST_FOREACH(SStructMemberSp entry, *members)
	{
		 if ( entry ) 
		 {
			 if ( entry->GetName() == name )
			 {
				 return true;
			 }
		 }
	}

	return false;
}

int SStructTypeDescriptor::GetMemberPosition(string name)
{
	for(unsigned int i=0; i<members->size(); ++i)
	{
		SStructMemberSp entry = members->at(i);

		if ( entry ) 
		{
			if ( entry->GetName() == name )
			{
				return i;
			}
		}
	}

	throw SArgumentException("There is not a member with the name '" + name + "'");
}

SStructTypeDescriptorSp SStructTypeDescriptor::Create( string pluginName, string name, string description )
{
	return SStructTypeDescriptorSp( new SStructTypeDescriptor(pluginName, name, description) );
}